<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\index\controller;
use app\admin\logic\Log;
use app\common\model\PeopleRegister;
use app\common\model\TypeMajor;
use Elastica\Client;
use Elastica\Query\BoolQuery;
use Elastica\QueryBuilder;
use Elastica\Search;
use Elastica\Type\Mapping;
use think\Cache;
use think\Db;
use think\db\Query;
/**
 * 前端首页控制器
 */
class CreateNewTable extends IndexBase
{
    /**
     * 插入数据
     */
    public function insertData()
    {
        $table_type =[
            'project_bid'=>1,
            'project_contract'=>2,
            'project_finish_merge'=>3,
            'project_permit_merge'=>4
        ];
        $tables = [
            'project_bid'=>'pro_num,bid_type as type,bid_way,bid_unitname,bid_date,bid_money as money,bid_area,bid_pnum,pro_bid_num,company_url,bid_url,kid,data_level,daili_com_url',
            'project_contract'=>'pro_num,contract_url,pro_contract_num,contract_type as type,contract_denum,contract_money as money,contract_fabao_name,fabao_url,chengbao_name,chengbao_url,data_level,contract_bianhao,contractdate',
            'project_finish_merge'=>'pro_num,pro_fin_num,fin_money as money,fin_area,end_time,fin_denum,fin_pass_num,finish_url,data_level,pro_re_type,pro_size,bdate,span,length,corprole as type,company_name,company_url,per_juese,per_name,pro_fi_num,finish_id',
            'project_permit_merge'=>'pro_per_num,per_money as money,per_area,pass_time,per_denum,data_level,pro_num,per_url,corprole as type,company_name,company_url,per_juese,per_url_ext,per_name,pro_fi_num,permit_id'
        ];
        foreach ($tables as $table=>$field){
            $count = \db($table)->count();
            $size = 1000;
            $page =  ceil($count/$size);
            $i=0;
            while ($page>=0){
                $list = \db($table)->field($field)->limit($i,$size)->select();

                foreach ($list as &$v){
                    $v['table_type'] = $table_type[$table];
                }
                //var_dump($list);exit;
                $page--;
                $i++;
                $res = \db('project_merge')->insertAll($list);
                var_dump($res);
            }
        }
    }

    public function tableInsertForType()
    {
        $table_type =[
            'project_bid'=>1,
            'project_contract'=>2,
            'project_finish_merge'=>3,
            'project_permit_merge'=>4
        ];
        $tables = [
            'project_bid'=>'pro_num,bid_type as type,bid_way,bid_unitname,bid_date,bid_money as money,bid_area,bid_pnum,pro_bid_num,company_url,bid_url,kid,data_level,daili_com_url',
            'project_contract'=>'pro_num,contract_url,pro_contract_num,contract_type as type,contract_denum,contract_money as money,contract_fabao_name,fabao_url,chengbao_name,chengbao_url,data_level,contract_bianhao,contractdate',
            'project_finish_merge'=>'pro_num,pro_fin_num,fin_money as money,fin_area,end_time,fin_denum,fin_pass_num,finish_url,data_level,pro_re_type,pro_size,bdate,span,length,corprole as type,company_name,company_url,per_juese,per_name,pro_fi_num,finish_id',
            'project_permit_merge'=>'pro_per_num,per_money as money,per_area,pass_time,per_denum,data_level,pro_num,per_url,corprole as type,company_name,company_url,per_juese,per_url_ext,per_name,pro_fi_num,permit_id'
        ];
        foreach ($tables as $table=>$field){
            $count = \db($table)->count();
            $size = 1000;
            $page =  ceil($count/$size);
            $i=0;
            while ($page>=0){
                $list = \db($table)->field($field)->limit($i,$size)->select();

                foreach ($list as &$v){
                    $v['table_type'] = $table_type[$table];
                }
                //var_dump($list);exit;
                $page--;
                $i++;
                $res = \db('project_merge')->insertAll($list);
                var_dump($res);
            }
        }
    }
    
    public function getFiled()
    {
        $sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE table_name = 'jz_project_new' AND table_schema = 'ac_jianzhu';";
        $cloumns = \db()->query($sql);
        //var_dump($cloumns);exit;
        //implode(',',array_column($cloumns,'COLUMN_NAME'))
        echo implode(',',array_column($cloumns,'COLUMN_NAME'));
    }
}